<?php

namespace App\Http\Controllers\Api;

use App\Models\Artcat;
use App\Models\Article;
use App\Models\ArticleClass;
use App\Models\ArticleCommon;
use App\Models\Collection;
use App\Models\IntegralUsers;
use App\Models\LmColumn;
use App\Models\Provinces;
use App\Models\Subscribe;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NewsController extends Controller
{
    //
    public function index(Request $request)
    {
        $class_id = $request->get('class_id', 0);
        $keys = $request->get('keys');
        $model = Article::query();
        $model = $model->select('id', 'class_id', 'title', 'urls', 'thumb', 'click', 'status', 'sort', 'created_at');
        if ($class_id) {
            $model = $model->where('class_id', $class_id);
        }
        if($keys){
            $model = $model->where('title','like','%'.$keys.'%');
        }
        $model = $model->where('status', 1);
        $model = $model->orderBy('sort', 'asc');
        $model = $model->orderBy('click', 'desc');
        $re = $model->orderBy('created_at', 'desc')->paginate(10);
        foreach ($re as $value){
            if ($value['thumb']) {
                $value['thumb'] = getAliOssImage($value['thumb']);
            }
        }
        return showMsg('获取成功', 200, $re);
    }

    public function cate(Request $request)
    {
        $cate = ArticleClass::orderBy('sort', 'asc')->get();
        return showMsg('获取成功', 200, $cate);
    }

    public function details(Request $request)
    {
        $ids = $request->get('id');
        $re = Article::where('id', $ids)->first();
        $re['description'] = html2text($re['content']);
        if ($re['content']) {
            $re['content'] = replaceOssImgSrc($re['content']);
        }
        //$re['content'] = get_img_thumb_url(addBaseUrlToMedia($re['content'],'https://cloud.wxstores.com'));
        Article::where('id', $ids)->increment('views', 1);
        return showMsg('获取成功', 200, $re);
    }

    //点赞
    public function setClick(Request $request)
    {
        $ids = $request->get('id');
        $data['type'] = 0;
        $data['article_id'] = $ids;
        $data['uid'] = $this->uId;
        $re = Collection::where($data)->first();
        if (empty($re)) {
            Collection::create($data);
            Article::where('id', $ids)->increment('click', 1);
            return showMsg('点赞成功', 200);
        } else {
            Collection::where($data)->delete();
            return showMsg('取消成功', 201);
        }
    }

    //收藏
    public function setCollect(Request $request)
    {
        $ids = $request->get('id');
        $data['type'] = 1;
        $data['article_id'] = $ids;
        $data['uid'] = $this->uId;
        $re = Collection::where($data)->first();
        if (empty($re)) {
            Collection::create($data);
            return showMsg('收藏成功', 200);
        } else {
            Collection::where($data)->delete();
            return showMsg('取消成功', 201);
        }
    }

    //评论
    public function setCommon(Request $request){
        $article_id = $request->get('id');
        $content = $request->get('content');
        $data['article_id'] = $article_id;
        $data['user_id'] = $this->uId;
        $data['status'] = 0;
        $result = ArticleCommon::where($data)->first();
        if(empty($result)) {
            $data['user_name'] = $this->user['nickname'];
            $data['content'] = $content;
            ArticleCommon::create($data);
            return showMsg('评论成功，待审核中…', 200);
        }else{
            return showMsg('审核中，勿重复提交…', 201);
        }
    }

    public function goCommon(Request $request){
        $article_id = $request->get('id');
        $result = ArticleCommon::where('article_id',$article_id)->where('status',1)->limit(4)->get();
        return showMsg('评论成功，待审核中…', 200,$result);
    }

}
